package com.lkm.app_ui.m.Imple.fun;

import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.MyApplication;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.m.fun.DelegatePayTask;
import com.lkm.app_ui.m.vip.VipModule;
import com.lkm.frame.task.ProgressData;
import com.lkm.frame.task.ProgressDataImpl;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.javatool.NumberHelp;
import com.lkm.javatool.StringHelp;
import com.lkm.market2_0.entity.ResponEntity;
import com.lkm.market2_0.netapi.Api;
import com.lkm.market2_0.task.DefaultTaskExecutor;

import java.util.HashMap;
import java.util.Map;



public class DelegatePayTaskImple extends TaskAdapter<DelegatePayTask.DelegatePayTaskP, DelegatePayTask.DelegatePayTaskPR, Boolean> implements DelegatePayTask {

    public static String getID(){
        return DelegatePayTaskImple.class.getName();
    }

    public DelegatePayTaskImple() {
        super(new DefaultTaskExecutor(true), getID(), MyApplication.getInstance().getTaskManager2());
    }

    private volatile String payOrder;

    @Override
    public void doExecuting(DelegatePayTaskP param) {
        super.doExecuting(param);

        mTaskExecutor.executor(new Runnable() {
            @Override
            public void run() {
                ResponEntity<Map<String, String>> re = null;

//                    param.orderEntity.card = AppConst.getModuleManager().peekModule(VipModule.class).curVipInfo().getVipNo();


                try {
                    param.orderEntity.card = getParam().vipInfo.getVipNo();
                }catch (Exception e){}

                try {
                    re = ResponEntity.fromJson(
                            Api.delegatePay(RunTimeData.steelyardExtData.payOnlineInfo.payUrl, param.orderEntity.steelyardMac, "" + RunTimeData.steelyardInfo.getOrg(), param.orderEntity,param.payCode, param.payType, DelegatePayTaskImple.this)
                            , new com.google.gson.reflect.TypeToken<HashMap<String, String>>() {
                            }.getType(), "");
                } catch (Exception e) {

                    re = new ResponEntity().fail("");
                }

                if (re.isSuccess()) {

                    DelegatePayTaskPR delegatePayTaskPR = new DelegatePayTaskPR();

                    delegatePayTaskPR.step =  DelegatePayTaskPR.step_content;
                    delegatePayTaskPR.type = NumberHelp.toInt(re.getData().get("type"), 0);
                    delegatePayTaskPR.url = re.getData().get("content");
                    delegatePayTaskPR.payment_amount = re.getData().get("payment_amount");
                    delegatePayTaskPR.msg = re.getMsg();
                    payOrder = re.getData().get("ordernum");


                    if (delegatePayTaskPR.type == 3) {
                        String payinfo = re.getData().get("content");
                        payOk(payinfo);
                    } else {

                        loopResult();
                        progressUpdate(new ProgressDataImpl<>(0, 0, delegatePayTaskPR));
                    }

                    getParam().orderEntity.payment_amount = delegatePayTaskPR.payment_amount;

                } else {

                    DelegatePayTaskPR delegatePayTaskPR = new DelegatePayTaskPR();
                    delegatePayTaskPR.step = DelegatePayTaskPR.step_requestFail;
                    delegatePayTaskPR.msg = re.getMsg();
                    progressUpdate(new ProgressDataImpl<>(0, 0, delegatePayTaskPR));
                    complete(false);
                }


            }
        });


    }

    @Override
    public void onProgressUpdate(ProgressData<DelegatePayTaskPR> progressData) {

        try{
            String s = progressData.getData().payment_amount;
            if (!StringHelp.isEmpty(s)) {
                getParam().orderEntity.payment_amount = s;
            }
        }catch (Exception e){}
        super.onProgressUpdate(progressData);
    }


    private void loopResult() {
        if (!getIsContinue()) return;

        mTaskExecutor.executeOnDispatchDelayed(new Runnable() {
            @Override
            public void run() {
                if (!getIsContinue()) return;
                mTaskExecutor.executorDoAsyncExecuting(new Runnable() {
                    @Override
                    public void run() {
                        if (!getIsContinue()) return;
                        getResult();
                    }
                });

            }
        }, 2500);
    }

    private void getResult() {

        ResponEntity<Map<String, String>> re = null;
        try {
            re = ResponEntity.fromJson(
                    Api.delegatePayResult(RunTimeData.steelyardExtData.payOnlineInfo.payUrl, getParam().orderEntity.steelyardMac, "" + RunTimeData.steelyardInfo.getOrg(), payOrder, DelegatePayTaskImple.this)
                    , new com.google.gson.reflect.TypeToken<HashMap<String, String>>() {
                    }.getType(), "");
        } catch (Exception e) {
            re = new ResponEntity().fail("");
        }
        if (!getIsContinue()) return;
        if (re.isSuccess()) {

            int type = NumberHelp.toInt(re.getData().get("type"), 0);
            String payinfo = re.getData().get("content");
            String ordernum = re.getData().get("ordernum");

            if (type == 1) {

                loopResult();

            } else if (type == 2) {
                payOk(payinfo);
            } else {

                cancel();
            }

        } else {

            loopResult();
        }


    }



    private void payOk(String payinfo) {
        DelegatePayTaskPR delegatePayTaskPR = new DelegatePayTaskPR();
        delegatePayTaskPR.step = DelegatePayTaskPR.step_paySuccess;
        delegatePayTaskPR.payInfo = payinfo;
        progressUpdate(new ProgressDataImpl<>(0, 0, delegatePayTaskPR));
        complete(true);
    }
}
